//
//  PersonalCenterView.m
//  Comma
//
//  Created by dlios on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PersonalCenterView.h"
#import "PersonalCustomView.h"
#import "GlobalMethod.h"
#import "GlobalMacro.h"
#import "EditDetailViewController.h"
#import "AFNetWorkingYU.h"
#import "URLMacro.h"
#import "UIImageView+WebCache.h"
@implementation PersonalCenterView

- (void)setInforDic:(NSDictionary *)InforDic
{
    _InforDic = InforDic;
    [self createMyPersonalCenter];
}
- (void)createMyPersonalCenter
{
    [self setBackgroundColor:[UIColor whiteColor]];
    [self createPersonalInformation];
    [self createMyDynamic];
    [self createMyFonderInfomation];
    [self createSchoolInformation];
    [self createAnimationOfController];
//    [self MyDynamicAnimation];//页面加载后立即插入动画

}
//重写父类的左侧的button
- (void)creatLeftButtonWithTitle:(NSString *)leftTitle
{
    //图片大小 21 13
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftButton setFrame:CGRectMake(0, 20, 48, 44)];
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"Button_drawer(1)"] forState:UIControlStateNormal];
    
    [self.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.leftButton];
    

    
    
    NSLog(@"已经重新创建左侧按钮！！！！");
}
- (void)createPersonalInformation
{
    self.myPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(16, 80, 80, 80)];
    self.myPhoto.layer.cornerRadius = 40;
    self.myPhoto.layer.masksToBounds = YES;
    NSString *url = [self.InforDic objectForKey:@"img"];
    NSString *strUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", url];
    //得到图像数据
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl]];
    if ([url isEqualToString:@"default.png"]) {
        //添加占位图，注册手势的点击事件
        
        //照片保存本地？？？？
        [self.myPhoto setImage:[UIImage imageNamed:@"face"]];
    }
    else{
        [self.myPhoto setImage:[UIImage imageWithData:imgData]];
    }
    
    [self.myPhoto setBackgroundColor:[UIColor lightGrayColor]];
    [self.myPhoto.layer setMasksToBounds:YES];
    [self.myPhoto setContentMode:UIViewContentModeScaleAspectFill];
    [self.myPhoto setClipsToBounds:YES];
    self.myPhoto.layer.borderColor = (__bridge CGColorRef)(LINEERGRAY);
    self.myPhoto.layer.borderWidth = 0.5f;
    self.myPhoto.userInteractionEnabled = YES;
 
    [self addSubview:self.myPhoto];
    
    self.myName = [[UILabel alloc] initWithFrame:CGRectMake(16, 192, 80, 14)];
    [self.myName setBackgroundColor:[UIColor clearColor]];
    [self.myName setTextAlignment:NSTextAlignmentCenter];
    [self.myName setFont:[UIFont systemFontOfSize:14]];
    [self.myName setText:[self.InforDic objectForKey:@"nick"]];
    [self addSubview:self.myName];

    [self PersonalInformationSeperateLine];

}


- (void)createMyFonderInfomation
{
    self.profile = [[UILabel alloc] initWithFrame:CGRectMake(144, 105, 200, 50)];
    if (iPhone6Plus) {
        [self.profile setFrame:CGRectMake(144, 105, 250, 50)];
    }
    if (iPhone5) {
        [self.profile setFrame:CGRectMake(144, 95, 150, 65)];
    }
    [self.profile setBackgroundColor:[UIColor clearColor]];
    [self.profile setTextColor:BLACKCOLOR];
    [self.profile setFont:[UIFont systemFontOfSize:14]];
    [self.profile setNumberOfLines:0];
    [self.profile setText:[self.InforDic objectForKey:@"profile"]];
    [self addSubview:self.profile];
    
    UIImageView *goodPicture = [[UIImageView alloc] initWithFrame:CGRectMake(144, 192, 13, 18)];
    [goodPicture setImage:[UIImage imageNamed:@"Button_Praisesmall_effect"]];
    [goodPicture setBackgroundColor:[UIColor clearColor]];
    [self addSubview:goodPicture];
    
    
    UILabel *goodLabel = [[UILabel alloc] initWithFrame:CGRectMake(162, 192, 30, 15)];
    NSNumber *goodNumber = [self.InforDic objectForKey:@"good"];
    NSString *good = [NSString stringWithFormat:@"%@",goodNumber];
    [goodLabel setText:good];
 
    [goodLabel setFont:[UIFont systemFontOfSize:11]];
    [goodLabel setBackgroundColor:[UIColor clearColor]];
    [self addSubview:goodLabel];

   
    
    
    UIImageView *badPicture = [[UIImageView alloc] initWithFrame:CGRectMake(211, 192, 13, 18)];
    [badPicture setImage:[UIImage imageNamed:@"Button_Belittlesmall"]];
    [badPicture setBackgroundColor:[UIColor clearColor]];
    [self addSubview:badPicture];
    
    UILabel *badLabel = [[UILabel alloc] initWithFrame:CGRectMake(229, 192, 30, 15)];
   
        NSNumber *badNumber = [self.InforDic objectForKey:@"bad"];
        NSString *bad = [NSString stringWithFormat:@"%@", badNumber];
        [badLabel setText:bad];
    
    [badLabel setFont:[UIFont systemFontOfSize:11]];
    [badLabel setBackgroundColor:[UIColor clearColor]];
    [self addSubview:badLabel];
    

    
}
- (void)createSchoolInformation
{
    //年龄与性别需要拼接字符串
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, 230, SCREEN_WIDTH - 16, 195)];
    [infoView setBackgroundColor:[UIColor clearColor]];
    [self addSubview:infoView];
    NSArray *titleArray = [NSArray arrayWithObjects:@"院校", @"入学年份", @"年龄与性别", @"专业", @"系别", nil];
    for (int i = 0; i < 5; i++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,  38 * i, SCREEN_WIDTH - 32, 15) ];
        [titleLabel setTextColor:TEXTGREENCOLOR];
        [titleLabel setText:[titleArray objectAtIndex:i]];
        [titleLabel setFont:[UIFont systemFontOfSize:11]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [infoView addSubview:titleLabel];
    }
    
    //学校
    self.school = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH - 32, 20)];
    [self.school setTextColor:BLACKCOLOR];
    [self.school setFont:[UIFont systemFontOfSize:14]];
    [self.school setBackgroundColor:[UIColor clearColor]];
           [self.school setText:[self.InforDic objectForKey:@"school"]];
  
    
    [infoView addSubview:_school];
    
    //年级（入学年）
    self.grade = [[UILabel alloc] initWithFrame:CGRectMake(0, 53, SCREEN_WIDTH - 32, 20)];
    [self.grade setTextColor:BLACKCOLOR];
    [self.grade setFont:[UIFont systemFontOfSize:14]];
    [self.grade setBackgroundColor:[UIColor clearColor]];
    

        [self.grade setText:[self.InforDic objectForKey:@"grade"]];
  

    [self.grade setText:[self.InforDic objectForKey:@"grade"]];
    [infoView addSubview:_grade];

    //年龄和性别
    self.ageAndSex = [[UILabel alloc] initWithFrame:CGRectMake(0, 91, SCREEN_WIDTH - 32, 20)];
    [self.ageAndSex setTextColor:BLACKCOLOR];
    [self.ageAndSex setFont:[UIFont systemFontOfSize:14]];
    [self.ageAndSex setBackgroundColor:[UIColor clearColor]];
    //不知道年龄是什么类型的  暂且算是字符串
    [self.ageAndSex setText:[self getMyAgeAndSex]];
    [infoView addSubview:_ageAndSex];

    //专业
    self.major = [[UILabel alloc] initWithFrame:CGRectMake(0, 129, SCREEN_WIDTH - 32, 20)];
    [self.major setTextColor:BLACKCOLOR];
    [self.major setFont:[UIFont systemFontOfSize:14]];
    [self.major setBackgroundColor:[UIColor clearColor]];
           [self.major setText:[self.InforDic objectForKey:@"major"]];
    
    
    [infoView addSubview:_major];

    //系别
    self.dept = [[UILabel alloc] initWithFrame:CGRectMake(0, 167, SCREEN_WIDTH - 32, 20)];
    [self.dept setTextColor:BLACKCOLOR];
    [self.dept setBackgroundColor:[UIColor clearColor]];
    [self.dept setFont:[UIFont systemFontOfSize:14]];
           [self.dept setText:[self.InforDic objectForKey:@"dept"]];
  
    
    [infoView addSubview:_dept];


}
- (void)PersonalInformationSeperateLine
{
        [GlobalMethod drawLineWithStartPoint:CGPointMake(16, 425) EndPoint:CGPointMake(SCREEN_WIDTH, 425) inUIView:self];
}

- (void)createMyDynamic
{
    
    UIView *myDynamic = [[UIView alloc] initWithFrame:CGRectMake(16, 425, SCREEN_WIDTH - 16, 44)];
    [myDynamic setBackgroundColor:[UIColor clearColor]];
    [self addSubview:myDynamic];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 16, 44)];
    [self.button setBackgroundColor:[UIColor clearColor]];
    [myDynamic addSubview:self.button];
    
    self.myDynamic = [[UIButton alloc] initWithFrame:CGRectMake(0, 12, 60, 20)];
    [self.myDynamic setBackgroundColor:[UIColor clearColor]];
    [self.myDynamic setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [self.myDynamic setTitle:@"我的动态" forState:UIControlStateNormal];
    
    self.myDynamic.titleLabel.font = [UIFont systemFontOfSize:14];
    [myDynamic addSubview:self.myDynamic];
    
    //不动得小红点
    self.dynamic = [[UIView alloc] initWithFrame:CGRectMake(300, 18, 8, 8)];
    if (iPhone6Plus) {
        [self.dynamic setFrame:CGRectMake(344, 18, 8, 8)];
    }
    if (iPhone5) {
        [self.dynamic setFrame:CGRectMake(252, 18, 8, 8)];
    }
    self.dynamic.layer.cornerRadius = 4;
    self.dynamic.layer.masksToBounds = YES;
    [self.button addSubview:self.dynamic];
    
    //变化的小红点
    self.dynamiChange = [[UIView alloc] initWithFrame:CGRectMake(300, 18, 8, 8)];
    if (iPhone6Plus) {
        [self.dynamiChange setFrame:CGRectMake(344, 18, 8, 8)];
    }
    if (iPhone5) {
        [self.dynamiChange setFrame:CGRectMake(252, 18, 8, 8)];
    }
    self.dynamiChange.layer.cornerRadius = 5;
    self.dynamiChange.layer.masksToBounds = YES;
    [self.button addSubview:self.dynamiChange];
    
    //向右的箭头
    UIImageView *toRight = [[UIImageView alloc] initWithFrame:CGRectMake(310, 0, 44, 44)];

    if (iPhone6Plus) {
        [toRight setFrame:CGRectMake(SCREEN_WIDTH - 60, 0, 44, 44)];
    }
    if (iPhone5) {
        [toRight setFrame:CGRectMake(SCREEN_WIDTH - 60, 0, 44, 44)];
    }
    [toRight setImage:[UIImage imageNamed:@"Button_updown_open"]];
     toRight.transform = CGAffineTransformMakeRotation(M_PI_2);
    [myDynamic addSubview:toRight];
    
    [self myDynamicSeperateLine];
}
- (void)myDynamicSeperateLine
{
    [GlobalMethod drawLineWithStartPoint:CGPointMake(16, 469) EndPoint:CGPointMake(SCREEN_WIDTH, 469) inUIView:self];
}
//开启定时器（不是计时器）
- (void)addTimer
{
    [self.dynamic setBackgroundColor:REDCOLOR];
    [self.dynamiChange setBackgroundColor:REDCOLOR];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.3 target:self selector:@selector(MyDynamicAnimation) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//关闭定时器、、在controller里直接关闭就可以了
- (void)removeTimer
{
    [self.dynamic setBackgroundColor:[UIColor clearColor]];
    [self.dynamiChange setBackgroundColor:[UIColor clearColor]];
    [self.timer invalidate];
}

//动画效果
- (void)MyDynamicAnimation
{
    
    [UIView animateWithDuration:1.2 animations:^{
        [self.dynamiChange setAlpha:0];
         self.dynamiChange.layer.cornerRadius = 12;
        
        [self.dynamiChange setFrame:CGRectMake(292, 10, 24, 24)];
        if (iPhone6Plus) {
            [self.dynamiChange setFrame:CGRectMake(336, 10, 24, 24)];
        }
        if (iPhone5) {
            [self.dynamiChange setFrame:CGRectMake(SCREEN_WIDTH - 68, 10, 24, 24)];
        }
//        CGFloat radius = self.dynamiChange.frame.size.width/2;
       
    } completion:^(BOOL finished) {
        [self.dynamiChange setFrame:CGRectMake(300, 18, 8, 8)];
        if (iPhone6Plus) {
            [self.dynamiChange setFrame:CGRectMake(344, 18, 8, 8)];
        }
        if (iPhone5) {
            [self.dynamiChange setFrame:CGRectMake(252, 18, 8, 8)];
        }
        self.dynamiChange.layer.cornerRadius = 4;
        [self.dynamiChange setAlpha:1];
        
    }];
}


//没用了
/*
#pragma mark - 计算出生日月份天数
- (NSInteger)calculateYear:(NSInteger)year MonthDays:(NSInteger)month days:(NSInteger)days
{
     NSInteger monthDay = 0;
    //闰年
    if ((year % 4 == 0 || year % 400 == 0) && year % 100 != 0) {
       
        NSArray *monthArray = [NSArray arrayWithObjects:@"31",@"29",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
        for (int i = 0; i < month; i++) {
            monthDay = monthDay + [[monthArray objectAtIndex:i]  integerValue];
        }
        NSLog(@"monthdays = %ld",monthDay);
    }
    else //平年
    {
        
        NSArray *monthArray = [NSArray arrayWithObjects:@"31",@"28",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
        for (int i = 0; i < month; i++) {
            monthDay = monthDay + [[monthArray objectAtIndex:i]  integerValue];
        }
        NSLog(@"monthdays = %ld",monthDay);
    }
    
    return (monthDay + days);
}
*/

- (void)createAnimationOfController
{
    self.backView = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 108)];
    [self.backView setBackgroundColor:[UIColor blackColor]];
    [self.backView setAlpha:0.6];
    [self addSubview:self.backView];
    
    self.chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT + 108, SCREEN_WIDTH, 88)];
    [self.chooseView setBackgroundColor:[UIColor clearColor]];
    [self addSubview:self.chooseView];
    
    self.fromCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [self.fromCamera setBackgroundColor:[UIColor whiteColor]];
    [self.fromCamera setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [self.fromCamera setTitle:@"相机" forState:UIControlStateNormal];
    [self.chooseView addSubview:_fromCamera];
    
    UIView *seperateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, SCREEN_WIDTH, 0.5)];
    [seperateLine setBackgroundColor:BLACKCOLOR];
    [self.chooseView addSubview:seperateLine];
    
    self.fromAlbum = [[UIButton alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 44)];
    [self.fromAlbum setBackgroundColor:[UIColor whiteColor]];
    [self.fromAlbum setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [self.fromAlbum setTitle:@"相册" forState:UIControlStateNormal];
    [self.chooseView addSubview:_fromAlbum];

}

- (NSString *)getMyAgeAndSex
{
    NSString *age = [self.InforDic objectForKey:@"birth"];
    age = [self getMyAge:age];
    NSString *gender = [self.InforDic objectForKey:@"gender"];
    
    if (([age isEqualToString:@""] || age == nil) && (![gender isEqualToString:@""] || gender != nil)) {
        NSString *ageAndGender = [NSString stringWithFormat:@"    %@", gender];
        return ageAndGender;
    }
    else if ( ([gender isEqualToString:@""] || gender == nil) && (![age isEqualToString:@""] || age != nil)) {
        NSString *ageAndGender = [NSString stringWithFormat:@"%@   ", age];
        return ageAndGender;
    }
    else if ( ([age isEqualToString:@""] || age == nil) && ([gender isEqualToString:@""] || gender == nil))
    {
        
        return nil;
    }
    else
    {
        NSString *ageAndGender = [NSString stringWithFormat:@"%@ %@", age, gender];
        return ageAndGender;
    }

}
#pragma mark - 根据生日 获取年龄
- (NSString *)getMyAge:(NSString *)birthday
{
    
    NSString *timeStr = birthday;
    //得到数组的年月日
    NSArray *myBirth = [birthday componentsSeparatedByString:@"-"];
    NSLog(@"%@", myBirth);
    
    
    
    
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"YY-MM-dd"];
    NSDate *d=[date dateFromString:timeStr];
    
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/86400>1)
    {
#pragma mark - fuck!! 次数不需要计算了   因为是从自己生日那天算起的 除以365的时候余数是从生日开始的
        /*
         NSInteger day =  cha / 86400;
         NSInteger year = day / 365;
         NSInteger myDays = [self calculateYear:[[myBirth firstObject] integerValue] MonthDays:[[myBirth objectAtIndex:1] integerValue] days:[[myBirth lastObject] integerValue]];
         if ((day % 365) > myDays) {
         year ++;
         }
         */
        
        timeString = [NSString stringWithFormat:@"%f", cha/86400/365 + 1];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@", timeString];
    }
    
    return timeString;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}



@end
